//
//  MineViewController.m
//  movie
//
//  Created by 白浩洋 on 2018/4/2.
//  Copyright © 2018年 白浩洋. All rights reserved.
//

#import "MineViewController.h"
#import "GKCover.h"
#import "NBAFeedBackViewController.h"
#import "guanyuViewController.h"
#import "liulanViewController.h"
#import "shenheshoucangViewController.h"
@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UIButton *logOut;
@property (nonatomic,strong)UIView *headView;
@property (nonatomic,strong)UIImageView *iconImage;
@property (nonatomic,strong)UILabel *nickName;
@property (nonatomic,strong)UITableView *tabView;
@property (strong,nonatomic)NSMutableArray *dateSource;
@end

@implementation MineViewController
{
    UIImageView *iConIamge;
    UITableView *tabview;
    UILabel *nameLabel;
    UILabel *starLabel;
    UILabel *slabe;
    
}
- (void)tapClick{
    [GKCover hideCover];
}
- (void)aboutClick{
    guanyuViewController *gvc = [[guanyuViewController alloc]init];
    gvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:gvc animated:YES];
}
- (void)logOutClick{
    [BmobUser logout];
    NSUserDefaults *nsud = [NSUserDefaults standardUserDefaults];
    [nsud removeObjectForKey:@"username"];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"logOut" object:nil];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getStar];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addCustomTitleWithTitle:@"个人中心"];
    [self setUpUI];
}
- (void)setUpUI {
    [self headView];
    self.tabView.backgroundColor = [UIColor clearColor];
    [self logOut];
}
- (void)getStar{
    BmobUser *user = [BmobUser currentUser];
    //查找GameScore表
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"star"];
    //查找GameScore表里面id为0c6db13c的数据
    [bquery whereKey:@"userid" equalTo:user.username];
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        for (BmobObject *obj in array) {
            NSString *sum = [obj objectForKey:@"star"];
            [NBATools setstar:[sum integerValue]];
            __weak __typeof(self) weakself= self;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakself.tabView reloadData];
                });
            break;
        }
    }];
}
- (UIView *)headView{
    if(!_headView){
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, PXChange(154))];
        _headView.backgroundColor = [UIColor colorWithHexString:BH_Color_Main];
        [_headView addSubview:self.iconImage];
        [_headView addSubview:self.nickName];
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,PXChange(24), PXChange(34))];
        image.image = [UIImage imageNamed:@"mine_headr_jiantou"];
        image.center = CGPointMake(ScreenWidth - PXChange(40) - image.width/2.0f, self.headView.height/2.0f);
        [_headView addSubview:image];
        [self.view addSubview:_headView];
    }
    return _headView;
}
- (UIImageView *)iconImage{
    if(!_iconImage){
        _iconImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, PXChange(100), PXChange(100))];
        _iconImage.image = [UIImage imageNamed:@"user_icon"];
        _iconImage.clipsToBounds = YES;
        _iconImage.layer.cornerRadius = _iconImage.width/2.0f;
        _iconImage.center = CGPointMake(_iconImage.width/2.0f + PXChange(30), self.headView.height/2.0f);
    }
    return _iconImage;
}
- (UILabel *)nickName{
    if(!_nickName){
        BmobUser *user = [BmobUser currentUser];
        _nickName = [[UILabel alloc]initWithFrame:CGRectZero];
        _nickName.textColor = [UIColor whiteColor];
        _nickName.font = [UIFont fontWithName:@"PingFangSC-Regular" size:PXChange(48)];
        _nickName.text = user.username;
        [_nickName sizeToFit];
        _nickName.center = CGPointMake(_nickName.width/2.0f + PXChange(30) + _iconImage.right, self.headView.height/2.0f);
    }
    return _nickName;
}
- (UITableView *)tabView{
    if(!_tabView){
        _tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.headView.bottom +PXChange(20), ScreenWidth, PXChange(1000))];
        _tabView.delegate = self;
        _tabView.dataSource =self;
        [_tabView setScrollEnabled:NO];
        _tabView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        [self.view addSubview:_tabView];
    }
    return _tabView;
}
-(NSMutableArray *)dateSource{
    if(!_dateSource){
        if(![NBATools SHState]){
        _dateSource = [NSMutableArray arrayWithArray:@[@"意见反馈",@"关于"]];
        }else{
         _dateSource = [NSMutableArray arrayWithArray:@[@"我的星星",@"我的收藏",@"意见反馈",@"关于",@"联系客服"]];
        }
    }
    return _dateSource;
}
#pragma mark - tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dateSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return PXChange(106);
}
#pragma mark - tableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = self.dateSource[indexPath.row];
    cell.textLabel.textColor = [UIColor colorWithHexString:@"#333333"];
    cell.textLabel.font = [UIFont systemFontOfSize:PXChange(28)];
    if(indexPath.row == 0 && [NBATools SHState]){
        cell.detailTextLabel.text = [NSString stringWithFormat:@"余额:%zd",[NBATools getstar]];
        cell.detailTextLabel.textColor = [UIColor colorWithHexString:BH_Color_Main];
        cell.accessoryType=UITableViewCellAccessoryNone;
    }
    if(indexPath.row == self.dateSource.count - 1 && ![NBATools SHState]){
        cell.detailTextLabel.text = [NSString stringWithFormat:@"2471573973"];
        cell.detailTextLabel.textColor = [UIColor colorWithHexString:BH_Color_Main];
        cell.accessoryType=UITableViewCellAccessoryNone;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:{
            //充值
            if(![NBATools SHState]){
                NBAFeedBackViewController *NFB = [[NBAFeedBackViewController alloc]init];
                NFB.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:NFB animated:YES];
            }
        }
            break;
        case 1:{
            if(![NBATools SHState]){
                [self aboutClick];
            }else{
                liulanViewController *NFB = [[liulanViewController alloc]init];
                NFB.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:NFB animated:YES];
            }
        }
            break;
        case 2:{
//            if([NBATools SHState]){
//                shenheshoucangViewController *shvc = [[shenheshoucangViewController alloc]init];
//                shvc.hidesBottomBarWhenPushed = YES;
//                [self.navigationController pushViewController:shvc animated:YES];
//            }else{
            NBAFeedBackViewController *NFB = [[NBAFeedBackViewController alloc]init];
            NFB.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:NFB animated:YES];
//            }
        }
            break;
        case 3:{
            [self aboutClick];
//            liulanViewController *NFB = [[liulanViewController alloc]init];
//            NFB.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:NFB animated:YES];
        }
            break;
        case 4:{
            NSURL *url = [NSURL URLWithString:@"mqq://im/chat?chat_type=wpa&uin=2471573973&version=1&src_type=web"];
            BOOL hasInstalled = [[UIApplication sharedApplication] canOpenURL:url];
            
            if (!hasInstalled) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" 提示 " message:@" 请先安装 QQ" delegate:nil cancelButtonTitle:@" 确定 " otherButtonTitles:nil, nil];
                [alert show];
            } else {
                [[UIApplication sharedApplication] openURL:url];
            }
            
        }
            break;
            
        default:
            break;
    }
}
- (UIButton *)logOut{
    if(!_logOut){
        _logOut = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, PXChange(600), PXChange(100))];
        [_logOut.titleLabel setFont:[UIFont systemFontOfSize:PXChange(36)]];
        [_logOut setTitle:@"退出登录" forState:UIControlStateNormal];
        [_logOut setBackgroundColor:[UIColor colorWithHexString:@"#333333"]];
        _logOut.clipsToBounds = YES;
        _logOut.layer.cornerRadius = _logOut.height/2.0f;
        [_logOut addTarget:self action:@selector(logOutClick) forControlEvents:UIControlEventTouchUpInside];
        _logOut.backgroundColor = [UIColor colorWithHexString:BH_Color_Main];
        [_logOut setTitleColor:[UIColor colorWithHexString:@"#ffffff"] forState:UIControlStateNormal];
        _logOut.center = CGPointMake(ScreenWidth/2.0f, ScreenHeight - 64 - 44 - _logOut.height/2.0f - PXChange(140) - iPhoneXTabHeigth);
        [self.view addSubview:_logOut];
    }
    return _logOut;
}
@end
